global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A34_borusyak_hull_correction.log, replace name(tabl)

capture noi { 
        

    * v14
    * Borusyak and Hull SIMULATIONS (adapted from ADAO et. al)

    qui do ${code_dir}/config/tabletools.do

    cap program drop vars
    program vars
        hasvar stock* spill*, local("stockspill") yes("\yes") no("\no")
        hasvar GDPGAP*, local("gdpgap") yes("\yes") no("\no")
        hasvar VAEMP*, local("vaemp") yes("\yes") no("\no")
        hasvar GDPPC*, local("gdppc") yes("\yes") no("\no")
    end program

    local pvers country

    * Merge main reg dataset with prepared mu variables, restrict to baseline firms
    use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
    mmerge BvD year using ${dataset_dir}/montecarlo/storage_mu_`pvers'_4000.dta, unmatched(master)
    mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(none)

    * Prepare the regressions
    estimates clear
    local yy=1995

    clonevar LSW = lswMPm_`yy'_a
    gen LSW_mu_diff = LSW-LSW_mu
    clonevar HSW = hswMPm_`yy'_a
    gen HSW_mu_diff = HSW-HSW_mu
    clonevar VAEMP = vaempMPm_`yy'_a
    gen VAEMP_mu_diff = VAEMP - VAEMP_mu
    clonevar GDPGAP = lngdpgap_`yy'_a
    gen GDPGAP_mu_diff = GDPGAP- GDPGAP_mu
    clonevar GDPPC = gdppcMPm_`yy'_a
    gen GDPPC_mu_diff = GDPPC - GDPPC_mu
    clonevar stockown = k${depvar}_${ttt} 
    clonevar stockownzero = k${depvar}_${ttt}0  
    clonevar stockother = kNOT_${depvar}_${ttt} 
    clonevar stockotherzero = kNOT_${depvar}_${ttt}0
    clonevar spilloversown = spill${depvar}${ttt}_`yy'_a
    clonevar spilloversother = spillN${depvar}${ttt}_`yy'_a
    clonevar spilloversownzero = spill${depvar}${ttt}_`yy'_a0
    clonevar spilloversotherzero = spillN${depvar}${ttt}_`yy'_a0
    bys lse_id : egen _total_${depvar}_${ttt}_`yy' = sum(${depvar}_${ttt}) if year>=`yy'+2 & year <= 2009+2
    bys lse_id : egen total_${depvar}_${ttt}_`yy' = max(_total_${depvar}_${ttt}_`yy')
    sort lse_id year
    tab year, gen(YY_)
    egen yearctry = group(year country_shr_1995) if year <= 2009
    egen yearindustry = group(year industry) if year <= 2009
    gen lswratiohsw = LSW - HSW
    xtset lse_id year
    sort lse_id year

    * 1) LSW_mu_diff HSW_mu_diff GDPGAP_mu_diff | F+IY
    ppmlhdfe F2.${depvar}_${ttt} LSW_mu_diff HSW_mu_diff GDPGAP_mu_diff stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) vce(cluster lse_id)
    vars
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\no"
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col`yy'_1

    * 2) LSW_mu_diff HSW_mu_diff GDPGAP_mu_diff VAEMP | F+IY
    ppmlhdfe F2.${depvar}_${ttt} LSW_mu_diff HSW_mu_diff GDPGAP_mu_diff VAEMP_mu_diff stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) vce(cluster lse_id)
    vars
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\no"
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col`yy'_2

    * 3) GDPGAP_mu_diff + GDPPC_mu_diff | F+IY
    ppmlhdfe F2.${depvar}_${ttt} LSW_mu_diff HSW_mu_diff GDPGAP_mu_diff GDPPC_mu_diff stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) vce(cluster lse_id)
    vars
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\no"
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col`yy'_3

    * 4) GDPGAP_mu_diff | F+IY+CY
    ppmlhdfe F2.${depvar}_${ttt} LSW_mu_diff HSW_mu_diff GDPGAP_mu_diff stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
    vars
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col`yy'_4

    * 5) GDPGAP_mu_diff + VAEMP_mu_diff | F+IY+CY
    ppmlhdfe F2.${depvar}_${ttt} LSW_mu_diff HSW_mu_diff GDPGAP_mu_diff VAEMP_mu_diff stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
    vars
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col`yy'_5

    * 6) GDPGAP_mu_diff + GDPPC_mu_diff | F+IY+CY
    ppmlhdfe F2.${depvar}_${ttt} LSW_mu_diff HSW_mu_diff GDPGAP_mu_diff GDPPC_mu_diff stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
    vars
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col`yy'_6

    if "`pvers'" != "countrydrop" {

        * Prepare foreign regression variables
        foreach shr in shr4 {
            clonevar LSW_foreign = lswMPm_`shr'_foreign_`yy'_a
            gen LSW_foreign_mu_diff = LSW_foreign - LSW_foreign_mu
            clonevar HSW_foreign = hswMPm_`shr'_foreign_`yy'_a
            gen HSW_foreign_mu_diff = HSW_foreign - HSW_foreign_mu
            clonevar GDPGAP_foreign = lngdpgap_shr_foreign_`yy'_a
            gen GDPGAP_foreign_mu_diff = GDPGAP_foreign - GDPGAP_foreign_mu
            clonevar GDPPC_foreign = gdppcMPm_`shr'_foreign_`yy'_a
            gen GDPPC_foreign_mu_diff = GDPPC_foreign - GDPPC_foreign_mu
            clonevar VAEMP_foreign = vaempMPm_`shr'_foreign_`yy'_a
            gen VAEMP_foreign_mu_diff = VAEMP_foreign - VAEMP_foreign_mu
        }

        * 7) GDPGAP_mu_diff | F+IY+CY | foreign
        ppmlhdfe F2.${depvar}_${ttt} LSW_foreign_mu_diff HSW_foreign_mu_diff GDPGAP_foreign_mu_diff stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        vars
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_7

        * 8) GDPGAP_mu_diff + VAEMP_mu_diff | F+IY+CY | foreign
        ppmlhdfe F2.${depvar}_${ttt} LSW_foreign_mu_diff HSW_foreign_mu_diff GDPGAP_foreign_mu_diff VAEMP_foreign_mu_diff stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        vars
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_8

        * 9) GDPGAP_mu_diff + GDPPC_mu_diff | F+IY+CY | foreign
        ppmlhdfe F2.${depvar}_${ttt} LSW_foreign_mu_diff HSW_foreign_mu_diff GDPGAP_foreign_mu_diff GDPPC_foreign_mu_diff stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
        vars
        estadd local f "\yes"
        estadd local iy "\yes"
        estadd local cy "\yes"
        estadd local obs "{\num{`e(N)'}}"
        estadd local firms "{\num{`e(N_clust)'}}"
        estimates store col`yy'_9
    }

    if "`pvers'" != "country" { 
        local pvers_ind "_`pvers'"
    }

    setlabels

    dhoztab * using ${tab_dir}/appendix/Table_A34_borusyak_hull_correction`pvers_ind'.tex,  b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines nomtitles sfmt(a1) scalars("has_gdpgap \noalign{\medskip} GDP gap" "has_vaemp Labor productivity" "has_gdppc GDP per capita" "has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label mgroups("Domestic and foreign" "Foreign", pattern(1 0 0 0 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(*zero spillover* stock* GDPGAP* GDPPC* VAEMP* _cons) noobs rename(LSW_mu_diff LSW LSW_foreign_mu_diff LSW HSW_mu_diff HSW HSW_foreign_mu_diff HSW) ///
    depvar("Auto95") order(LSW HSW GDPGAP VAEMP GDPPC stockown stockother spilloversown spilloversother) ///
    notes("This table replicates the baseline regression applying the correction suggested by Borusyak and Hull (2021). We sample with replacement the entire path of log macroeconomic variables (wages, labor productivity, GDP per capita, and GDP gap) for each firm with 4000 draws, take the average value, and subtract it from the original macroeconomic variable.")
    *again, brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}